/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 */
package com.google.code.bigyang.nio;

import java.nio.ByteBuffer;
import java.nio.channels.SelectableChannel;
import java.util.Set;
import com.google.code.bigyang.base.Filter;
import com.google.code.bigyang.base.impl.AcceptorImpl;
import com.google.code.bigyang.buffer.NioBufferFacade;
import com.google.code.bigyang.buffer.NioBufferImpl;
import com.google.code.bigyang.config.Configuration;
import com.google.code.bigyang.util.ByteBufferUtil;

public abstract class NioAcceptor extends AcceptorImpl {

//	public abstract void accept();
	
	
	protected SelectorSet manager = new SelectorSet();
	
	protected Configuration config;
	
	public NioAcceptor(Configuration config) {
		this.config = config;
	}
	
	protected abstract NioSession createSessionInstance();
	
	protected NioSession createSession(SelectableChannel channel) {
		NioSession session = createSessionInstance();
		session.setChannel(channel);
		
		ByteBuffer bb = ByteBufferUtil.allocateByteBuffer(config.getBufferSize(), config.isDirect());
		NioBufferFacade buffer = new NioBufferFacade(new NioBufferImpl(bb));
		session.setBuffer(buffer);
		session.setManager(manager);
		Set<Filter> set = config.getFilters();
		for(Filter f:set) {
			session.addFilter(f);
		}
		session.addHanlder(config.getHanlder());
		return session;
	}
	

}
